<?php

class Login extends CI_Controller
{
    function index()
    {
        $data['contenido_principal'] = 'vista_login';
        $this->load->view('includes/plantilla', $data);
    }
	
    function validar_usuario()
    {
        $this->form_validation->set_rules('rut','Rut','trim|required|min_length[8]|numeric');
        $this->form_validation->set_rules('password','Password','trim|required|min_length[6]');

        $this->form_validation->set_message('min_length', 'El campo %s debe tener al menos %s caracteres.');	
        $this->form_validation->set_message('required', 'El campo %s es requerido para continuar.');
        $this->form_validation->set_message('numeric', 'El campo %s solo acepta valores numericos.');

        if($this->form_validation->run() == FALSE)
        {
            $this->index();
        }
        else
        {
            $this->load->model('modelo_usuario');
            $query = $this->modelo_usuario->validar();
            if($query)
            {
                $data = array(
                'rut' => $this->input->post('rut'),
                'esta_logueado' => true
                );

                $this->session->set_userdata($data);
                redirect('sitio');
            }		
            else
            {
                $this->session->set_flashdata('error', 'Usuario o Password incorrectos.');
                redirect('login');
            }
        }	
    }
}